package pointandclick;

import java.util.ArrayList;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import static org.lwjgl.opengl.GL11.*;

public class PointAndClick {

    int WIDTH = 1280;
    int HEIGHT = 720;
    int fps;
    long lastFPS;
    ArrayList<float[]> coordinateArray = new ArrayList();
    boolean dragLine = false;
    int interation = 5;
    int length = 20;
    int mouseCount;

    public static void main(String[] args) {
        PointAndClick pointAndClick = new PointAndClick();
        pointAndClick.start();

    }

    public void start() {
        try {
            Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
            Display.create();
        } catch (LWJGLException e) {
            e.printStackTrace();
            System.exit(0);
        }
        initGL();
        lastFPS = getTime();
        updateFPS();

        while (!Display.isCloseRequested()) {
            updateFPS();
            processMouse();
            processKeybaord();
            update();
            render();
            Display.update();
            //Display.sync(60);
        }
    }

    public void processKeybaord() {
        while (Keyboard.next()) {
            if (Keyboard.getEventKeyState()) {
                if (Keyboard.getEventKey() == Keyboard.KEY_R) {
                    glClear(GL_COLOR_BUFFER_BIT);
                    coordinateArray.clear();
                    dragLine = false;
                }
                if (Keyboard.getEventKey() == Keyboard.KEY_C) {
                    glColor3f((float) Math.random(), (float) Math.random(), (float) Math.random());
                }
                
            }
        }
    }

    public void processMouse() {
        while (Mouse.next()) {
            if (Mouse.getEventButtonState()) {
                if (Mouse.getEventButton() == 0) {
                    mouseCount++;
                    System.out.println(mouseCount);
                    dragLine = !dragLine;
                    if (dragLine) {
                        coordinateArray.add(getMouseXY());
                        coordinateArray.add(getMouseXY());
                    }
                }
             
            }
        }
    }

    public float[] getMouseXY() {
        return new float[]{Mouse.getX(), Mouse.getY()};
    }

    public void update() {
        if (dragLine) {
            coordinateArray.set(coordinateArray.size() - 1, getMouseXY());
        }
    }

    public void render() {
        glClear(GL_COLOR_BUFFER_BIT);
        if (!coordinateArray.isEmpty()) {
            for (int i = 0; i < coordinateArray.size(); i += 2) {
                drawLine(coordinateArray.get(i), coordinateArray.get(i + 1));
            }
        }
    }

    public void drawLine(float[] point1, float[] point2) {
        //glColor3f((float)Math.random(),(float)Math.random(),(float)Math.random());


        glBegin(GL_LINES);
        glVertex2f(point1[0], point1[1]);
        glVertex2f(point2[0], point2[1]);
        glEnd();
    }

    public void initGL() {
        glColor3f(.5f, .2f, .7f);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, WIDTH, 0, HEIGHT, 1, -1);
        glMatrixMode(GL_MODELVIEW);
    }

    public void updateFPS() {
        if (getTime() - lastFPS > 1000) {
            Display.setTitle("FPS: " + fps);
            fps = 0;
            lastFPS += 1000;
        }
        fps++;
    }

    public long getTime() {
        return System.nanoTime() / 1000000;
    }
}